package br.com.virtual.base.controller.menu;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.separator.Separator;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;

import br.com.virtual.base.controller.util.VirtualMB;
import br.com.virtual.cadastrar.model.Categoria;
import br.com.virtual.cadastrar.model.Grupo;
import br.com.virtual.cadastrar.service.CategoriaService;
import br.com.virtual.cadastrar.service.GrupoService;

@ManagedBean
@ViewScoped
public class MenuMB extends VirtualMB {

	private static final long serialVersionUID = 1L;
	private MenuModel menuModel;
	private MenuModel menuLateralModel;

	@EJB
	private GrupoService grupoService;
	@EJB
	private CategoriaService categoriaService;

	private MenuModel gerarMenuLateral() {

		menuLateralModel = new DefaultMenuModel();
		Long id;

		try {
			id = Long.valueOf(getRequestValue("g"));
		} catch (NumberFormatException e) {
			id = null;
		}

		if (id != null) {

			List<Categoria> categorias = this.categoriaService
					.getListByGrupo(this.grupoService.read(id));

			for (Categoria catPai : categorias) {

				MenuItem menuItem = new MenuItem();
				menuItem.setValue(catPai.getNome());
				menuItem.setUrl("/busca.jsf?g=" + id + "&c=" + catPai.getId());
				menuItem.setStyleClass("menu-item menu-item-categoria menu-item-categoria-pai");

				menuLateralModel.addMenuItem(menuItem);
				menuLateralModel.addSeparator(new Separator());

				for (Categoria catFilha : catPai.getFilhos()) {

					menuItem = new MenuItem();
					menuItem.setValue(catFilha.getNome());
					menuItem.setUrl("/busca.jsf?g=" + id + "&c="
							+ catFilha.getId());
					menuItem.setStyleClass("menu-item menu-item-categoria menu-item-categoria-filha");

					menuLateralModel.addMenuItem(menuItem);
					menuLateralModel.addSeparator(new Separator());
				}
			}

			if (menuLateralModel.getContents().size() > 0)
				menuLateralModel.getContents().remove(
						menuLateralModel.getContents().size() - 1);
		}
		return menuLateralModel;
	}

	private MenuModel gerarMenu() {

		List<Grupo> grupos = this.grupoService.carregar();
		MenuItem menuItem = new MenuItem();

		for (Grupo g : grupos) {
			menuItem.setValue(g.getNome());
			menuItem.setUrl("/busca.jsf?g=" + g.getId());
			menuItem.setStyleClass("menu-item menu-item-grupo");

			menuModel.addMenuItem(menuItem);
			menuItem = new MenuItem();
		}

		return menuModel;
	}

	public boolean exibirMenu() {

		Long g, c;

		try {
			c = Long.valueOf(this.getRequestValue("c"));
		} catch (NumberFormatException n) {
			c = null;
		}

		try {
			g = Long.valueOf(this.getRequestValue("g"));
		} catch (NumberFormatException n) {
			g = null;
		}

		if (c == null && g == null)
			return false;
		else
			return true;
	}

	/*
	 * getters and setters
	 */
	public MenuModel getMenuModel() {
		if (this.menuModel == null) {
			menuModel = new DefaultMenuModel();
			menuModel = this.gerarMenu();
		}
		return menuModel;
	}

	public void setMenuModel(MenuModel menuModel) {
		this.menuModel = menuModel;
	}

	public MenuModel getMenuLateralModel() {
		if (this.menuLateralModel == null) {

			menuLateralModel = this.gerarMenuLateral();
		}
		return menuLateralModel;
	}

	public void setMenuLateralModel(MenuModel menuLateralModel) {
		this.menuLateralModel = menuLateralModel;
	}

}
